{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在爬取第 1 页...\n",
      "已下载图片: 霸王别姬 - Farewell My Concubine.jpg\n",
      "已下载图片: 这个杀手不太冷 - Léon.jpg\n",
      "已下载图片: 肖申克的救赎 - The Shawshank Redemption.jpg\n",
      "已下载图片: 泰坦尼克号 - Titanic.jpg\n",
      "已下载图片: 罗马假日 - Roman Holiday.jpg\n",
      "已下载图片: 唐伯虎点秋香 - Flirting Scholar.jpg\n",
      "已下载图片: 乱世佳人 - Gone with the Wind.jpg\n",
      "已下载图片: 喜剧之王 - The King of Comedy.jpg\n",
      "已下载图片: 楚门的世界 - The Truman Show.jpg\n",
      "已下载图片: 狮子王 - The Lion King.jpg\n",
      "正在爬取第 2 页...\n",
      "已下载图片: V字仇杀队 - V for Vendetta.jpg\n",
      "已下载图片: 少年派的奇幻漂流 - Life of Pi.jpg\n",
      "已下载图片: 美丽心灵 - A Beautiful Mind.jpg\n",
      "已下载图片: 初恋这件小事 - สิ่งเล็กเล็กที่เรียกว่า...รัก.jpg\n",
      "已下载图片: 借东西的小人阿莉埃蒂 - 借りぐらしのアリエッティ.jpg\n",
      "已下载图片: 一一 - Yi yi: A One and a Two.jpg\n",
      "已下载图片: 美丽人生 - La vita è bella.jpg\n",
      "已下载图片: 海上钢琴师 - La leggenda del pianista sull'oceano.jpg\n",
      "已下载图片: 千与千寻 - 千と千尋の神隠し.jpg\n",
      "已下载图片: 迁徙的鸟 - The Travelling Birds.jpg\n",
      "正在爬取第 3 页...\n",
      "已下载图片: 黄金三镖客 - Il buono, il brutto, il cattivo..jpg\n",
      "已下载图片: 海洋 - Océans.jpg\n",
      "已下载图片: 我爱你 - 그대를 사랑합니다.jpg\n",
      "已下载图片: 阿飞正传 - Days of Being Wild.jpg\n",
      "已下载图片: 7号房的礼物 - 7번방의 선물.jpg\n",
      "已下载图片: 爱·回家 - 집으로....jpg\n",
      "已下载图片: 龙猫 - となりのトトロ.jpg\n",
      "已下载图片: 七武士 - 七人の侍.jpg\n",
      "已下载图片: 美国往事 - Once Upon a Time in America.jpg\n",
      "已下载图片: 完美的世界 - A Perfect World.jpg\n",
      "正在爬取第 4 页...\n",
      "已下载图片: 上帝之城 - Cidade de Deus.jpg\n",
      "已下载图片: 辩护人 - 변호인.jpg\n",
      "已下载图片: 忠犬八公物语 - ハチ公物語.jpg\n",
      "已下载图片: 海豚湾 - The Cove.jpg\n",
      "已下载图片: 英雄本色 - A Better Tomorrow.jpg\n",
      "已下载图片: 恐怖直播 - 더 테러 라이브.jpg\n",
      "已下载图片: 窃听风暴 - Das Leben der Anderen.jpg\n",
      "已下载图片: 时空恋旅人 - About Time.jpg\n",
      "已下载图片: 穿条纹睡衣的男孩 - The Boy in the Striped Pajamas.jpg\n",
      "已下载图片: 教父 - The Godfather.jpg\n",
      "正在爬取第 5 页...\n",
      "已下载图片: 萤火之森 - 蛍火の杜へ.jpg\n",
      "已下载图片: 素媛 - 소원.jpg\n",
      "已下载图片: 小鞋子 - بچههای آسمان.jpg\n",
      "已下载图片: 熔炉 - 도가니.jpg\n",
      "已下载图片: 大话西游之大圣娶亲 - A Chinese Odyssey Part Two - Cinderella.jpg\n",
      "已下载图片: 新龙门客栈 - New Dragon Gate Inn.jpg\n",
      "已下载图片: 触不可及 - Intouchables.jpg\n",
      "已下载图片: 钢琴家 - The Pianist.jpg\n",
      "已下载图片: 本杰明·巴顿奇事 - The Curious Case of Benjamin Button.jpg\n",
      "已下载图片: 倩女幽魂 - A Chinese Ghost Story.jpg\n",
      "正在爬取第 6 页...\n",
      "已下载图片: 哈利·波特与死亡圣器（下） - Harry Potter and the Deathly Hallows: Part 2.jpg\n",
      "已下载图片: 甜蜜蜜 - Comrades: Almost a Love Story.jpg\n",
      "已下载图片: 蝙蝠侠：黑暗骑士崛起 - The Dark Knight Rises.jpg\n",
      "已下载图片: 鬼子来了 - Devils on the Doorstep.jpg\n",
      "已下载图片: 无敌破坏王 - Wreck-It Ralph.jpg\n",
      "已下载图片: 致命魔术 - The Prestige.jpg\n",
      "已下载图片: 神偷奶爸 - Despicable Me.jpg\n",
      "已下载图片: 断背山 - Brokeback Mountain.jpg\n",
      "已下载图片: 怦然心动 - Flipped.jpg\n",
      "已下载图片: 驯龙高手 - How to Train Your Dragon.jpg\n",
      "正在爬取第 7 页...\n",
      "已下载图片: 飞屋环游记 - Up.jpg\n",
      "已下载图片: 黑客帝国3：矩阵革命 - The Matrix Revolutions.jpg\n",
      "已下载图片: 速度与激情5 - Fast Five.jpg\n",
      "已下载图片: 勇敢的心 - Braveheart.jpg\n",
      "已下载图片: 三傻大闹宝莱坞 - 3 Idiots.jpg\n",
      "已下载图片: 闻香识女人 - Scent of a Woman.jpg\n",
      "已下载图片: 末代皇帝 - The Last Emperor.jpg\n",
      "已下载图片: 风之谷 - 風の谷のナウシカ.jpg\n",
      "已下载图片: 大话西游之月光宝盒 - A Chinese Odyssey.jpg\n",
      "已下载图片: 放牛班的春天 - Les choristes.jpg\n",
      "正在爬取第 8 页...\n",
      "已下载图片: 当幸福来敲门 - The Pursuit of Happyness.jpg\n",
      "已下载图片: 幽灵公主 - もののけ姫.jpg\n",
      "已下载图片: 十二怒汉 - 12 Angry Men.jpg\n",
      "已下载图片: 搏击俱乐部 - Fight Club.jpg\n",
      "已下载图片: 疯狂原始人 - The Croods.jpg\n",
      "已下载图片: 阿凡达 - Avatar.jpg\n",
      "已下载图片: 哈尔的移动城堡 - ハウルの動く城.jpg\n",
      "已下载图片: 盗梦空间 - Inception.jpg\n",
      "已下载图片: 忠犬八公的故事 - Hachi: A Dog's Tale.jpg\n",
      "已下载图片: 拯救大兵瑞恩 - Saving Private Ryan.jpg\n",
      "正在爬取第 9 页...\n",
      "已下载图片: 活着 - To Live.jpg\n",
      "已下载图片: 机器人总动员 - WALL·E.jpg\n",
      "已下载图片: 天堂电影院 - Nuovo Cinema Paradiso.jpg\n",
      "已下载图片: 指环王2：双塔奇兵 - The Lord of the Rings: The Two Towers.jpg\n",
      "已下载图片: 指环王1：护戒使者 - The Lord of the Rings: The Fellowship of the Ring.jpg\n",
      "已下载图片: 射雕英雄传之东成西就 - The Eagle Shooting Heroes.jpg\n",
      "已下载图片: 蝙蝠侠：黑暗骑士 - The Dark Knight.jpg\n",
      "已下载图片: 无间道 - Infernal Affairs.jpg\n",
      "已下载图片: 教父2 - The Godfather: Part Ⅱ.jpg\n",
      "已下载图片: 加勒比海盗 - Pirates of the Caribbean: The Curse of the Black Pearl.jpg\n",
      "正在爬取第 10 页...\n",
      "已下载图片: 哈利·波特与魔法石 - Harry Potter and the Sorcerer's Stone.jpg\n",
      "已下载图片: 指环王3：王者无敌 - The Lord of the Rings: The Return of the King.jpg\n",
      "已下载图片: 黑客帝国 - The Matrix.jpg\n",
      "已下载图片: 剪刀手爱德华 - Edward Scissorhands.jpg\n",
      "已下载图片: 春光乍泄 - Happy Together.jpg\n",
      "已下载图片: 大闹天宫 - The Monkey King.jpg\n",
      "已下载图片: 天空之城 - 天空の城ラピュタ.jpg\n",
      "已下载图片: 音乐之声 - The Sound of Music.jpg\n",
      "已下载图片: 辛德勒的名单 - Schindler's List.jpg\n",
      "已下载图片: 魂断蓝桥 - Waterloo Bridge.jpg\n",
      "所有网页已爬取完成！\n"
     ]
    }
   ],
   "source": [
    "import codecs\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import csv\n",
    "import os\n",
    "\n",
    "if not os.path.exists(\"D:/desktop/movie_images\"):\n",
    "    os.makedirs(\"D:/desktop/movie_images\")        #####修改位置\n",
    "\n",
    "with open(\"D:/desktop/movies.csv\", \"ab+\") as fp:\n",
    "    fp.write(codecs.BOM_UTF8)####### 添加 BOM 头以避免在 Windows 下出现乱码，修改位置\n",
    "\n",
    "def save_to_csv(data):\n",
    "    with open('D:/desktop/movies.csv', 'a', newline='', encoding='utf-8') as f:\n",
    "        writer = csv.writer(f)\n",
    "        writer.writerow(data)####### 保存数据到CSV文件修改位置，改位置\n",
    "\n",
    "def scrape_movie_page(url):\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        soup = BeautifulSoup(response.text, 'html.parser')\n",
    "        movies = soup.find_all(\"div\", class_=\"el-card item m-t is-hover-shadow\")\n",
    "        for movie in movies:\n",
    "            name = movie.find(\"h2\", class_=\"m-b-sm\").text.strip()\n",
    "            score = movie.find(\"p\", class_=\"score m-t-md m-b-n-sm\").text.strip()\n",
    "            categories = [button.text.strip() for button in movie.find_all(\"button\", class_=\"el-button category el-button--primary el-button--mini\")]\n",
    "            release_date = movie.find_all(\"div\", class_=\"m-v-sm info\")[-1].text.strip()\n",
    "            countries_duration = movie.find(\"div\", class_=\"m-v-sm info\").find_all(\"span\", attrs={\"data-v-7f856186\": \"\"})\n",
    "            countries = countries_duration[0].text.strip()\n",
    "            duration = countries_duration[2].text.strip() if len(countries_duration) > 2 else \"未知\"\n",
    "            data = [name, score, categories, release_date, countries, duration]\n",
    "            save_to_csv(data)##爬取网页，不用动\n",
    "            img_tag = movie.find(\"img\", class_=\"cover\")\n",
    "            if img_tag:\n",
    "                img_url = img_tag[\"src\"].split(\"@\")[0]#####爬取图片\n",
    "                img_name = f\"{name}.jpg\"  # 以电影名字命名图片\n",
    "                img_path = os.path.join(\"D:/desktop/movie_images\", img_name)#####改位置\n",
    "                with open(img_path, 'wb') as img_file:\n",
    "                    img_file.write(requests.get(img_url).content)\n",
    "                print(f\"已下载图片: {img_name}\")\n",
    "            else:\n",
    "                print(\"未找到图片链接\")\n",
    "    else:\n",
    "        print(\"请求失败，状态码:\", response.status_code)\n",
    "\n",
    "def scrape_all_pages(base_url, num_pages):\n",
    "    for i in range(1, num_pages + 1):\n",
    "        url = f\"{base_url}/{i}\"\n",
    "        print(f\"正在爬取第 {i} 页...\")\n",
    "        scrape_movie_page(url)\n",
    "    print(\"所有网页已爬取完成！\")#爬取所有网页\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    base_url = \"https://ssr1.scrape.center/page\"\n",
    "    num_pages = 10  ######选择你需要爬取的页数，爬取范围为第一页到第（你填写的数字）页，一共是十页\n",
    "    scrape_all_pages(base_url, num_pages)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MyMLenvAllinOne",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
